<?php

namespace fankers\lib\github;

use fankers\api\githubApi;
use fankers\config\githubConfig;
use fankers\helper\Log;
use fankers\lib\base;

class Github extends base
{
    public function __construct($params)
    {
        return parent::__construct($params);
    }

    ## come on 授权code
    public function start()
    {
        $oauth  = githubApi::OAUTH_URI;## 授权地址
        $params['client_id']  = githubConfig::APP_ID;
        $params['redirect_uri'] = $this->redirect;
        $params['state']  = $this->randCharts(32);
        $params['scope']  = githubConfig::SCOPE;

        ## http_build_query 有urlencode的功能
        $url = $oauth."?".http_build_query($params);

        if (!isset($_GET['code'])) {
            header('Location:'.$url);
            exit();
        }
    }

    ## 获取 access_token
    public function token($params)
    {
        $data = [
            'client_id' => githubConfig::APP_ID,    #appKey
            'client_secret' => githubConfig::APP_KEY,# appSecret
        ];
        $tokenUrl = githubApi::ACCESS_TOKEN;    #token 地址
        if (is_array($params)) {
            $data = array_merge($data,$params);
        }

        $tokenInfo = $this->http->post($tokenUrl,$data);
        return $this->http->parseQueryParams($tokenInfo);
    }

    ## 获取用户信息
    public function info($params)
    {
        $infoUrl = githubApi::USER_INFO;
        $option = [ CURLOPT_USERAGENT => 'http://developer.github.com/v3/#user-agent-required' ]; #此处是个坑 搞不懂小洋人
        $userInfo = $this->http->get($infoUrl,$params,$option);
//        \helper\Log::debug($userInfo,'/tmp/fkk_qq_token_log');
        return $this->http->parseJSON($userInfo);
    }
}